//
//  ContactMessageSettingVc.m
//  wanzhaoIM
//
//  Created by wanzhao on 16/1/6.
//  Copyright © 2016年 liuyuantao. All rights reserved.
//

#import "ContactMessageSettingVc.h"
#import "MessageSettingCell.h"
#import "GroupMenberCell.h"
#import "CContactMgr.h"
#import "CContactModel.h"
#import "GroupMemberDetailVC.h"
#import "UIImageView+WebCache.h"
#import "CXmppRequestMgr.h"
#import "IMChatRecordController.h"
#import <objc/runtime.h>
@interface ContactMessageSettingVc ()<UITableViewDataSource, UITableViewDelegate,UIActionSheetDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) CContactMgr *contMgr;
@end

@implementation ContactMessageSettingVc

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"消息选项"];

    self.contMgr = [[CContactMgr alloc] init];
    
    _tableView = [[UITableView alloc] init];
    _tableView.frame = self.view.frame;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _tableView.backgroundColor = ClearColor;
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
    
    self.view.backgroundColor = CommonColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSInteger rows = 0;
    
    switch (section) {
        case 0:
            rows = 3;
            break;
        default:
            rows = 0;
            break;
    }
    return rows;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView              // Default is 1 if not implemented
{
    return 1;
}
// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"MessageSettingCell";
    static NSString *TextCellIdentifier = @"GroupMenberCell";
    UITableViewCell *cell = nil;
    if(indexPath.row == 1 || indexPath.row == 2)
    {
        //        SettingUserInfoCell.
        
        MessageSettingCell  *textSwtichCell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        if (textSwtichCell == nil) {
            textSwtichCell = [[[NSBundle mainBundle] loadNibNamed:@"MessgeSettingCell" owner:self options:nil] objectAtIndex:0];
        }
        [textSwtichCell.titleLabel setText:[self titleName:indexPath]];
        [textSwtichCell.seperatorLine setHidden:NO];
        if (indexPath.row == 1) {
            if (self.contact.messageSettingType == MESSAGESETTING_RECEVIEDANDREMIND) {
                textSwtichCell.subTitleLabel.text = @"接收并提示";
            } else if (self.contact.messageSettingType == MESSAGESETTING_RECEVIEDNOTREMIND) {
                textSwtichCell.subTitleLabel.text = @"接收而不提示";
            } else {
                textSwtichCell.subTitleLabel.text = @"只显示未读消息数目";
            }

        } else {
            textSwtichCell.subTitleLabel.text = @"";
        }
        
        cell = textSwtichCell;
    }
    else
    {
        GroupMenberCell *textCell = [tableView dequeueReusableCellWithIdentifier:TextCellIdentifier];
        if (textCell == nil) {
            textCell = [[[NSBundle mainBundle] loadNibNamed:@"GroupMenberCell" owner:self options:nil] objectAtIndex:0];
        }
        
        [textCell.seperatorLine setHidden:NO];
        if ([self.contact.yuLiuZiduan3 isEqualToString:@"isKefu"] && self.contact.isDel == 0) {
            [textCell.icon setImage:[UIImage imageNamed:@"head-kefu"]];
        } else {
            [textCell.icon sd_setImageWithURL:[NSURL URLWithString:[[[CXmppRequestMgr shareInstance] DownHeadFileWithContact:self.contact.UserID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]] placeholderImage:[UIImage imageNamed:@"80-80"]];
        }

        
        textCell.titleLabel.text = self.contact.UserName;
        textCell.Arrow.hidden = NO;
        cell = textCell;
    }
    
    return cell;
}

-(NSString *)titleName:(NSIndexPath *)indexPath
{
    NSString *title = nil;
    
    switch (indexPath.section) {
        case 0:
        {
            switch (indexPath.row) {
                case 1:
                    title = @"消息设置";
                    break;
                case 2:
                    title = @"消息记录";
                    break;
                    
                default:
                    break;
            }
        }
            break;
    }
    return title;
}




#pragma mark - UITableViewDelegate
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    CGFloat height = 0;
//    switch (section) {
//        case 0:
//            height = KSECTIONHEIGHT;
//            break;
//        default:
//            break;
//    }
//    return height;
//}
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
//{
//    CGFloat height = 0;
//    switch (section) {
//        case 0:
//            height = KSECTIONHEIGHT;
//            break;
//        default:
//            break;
//    }
//    return height;
//}


//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section   // custom view for header. will be adjusted to default or specified header height
//{
//    UIView *headerView = nil;
//    headerView = [[UIView alloc] init];
//    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, KSECTIONHEIGHT);
//    //    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
//    //    [headerView setBackgroundColor:[UIColor redColor]];
//    //    UIView *beginLine = [[UIView alloc] init];
//    //    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
//    //    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
//    //    [headerView addSubview:beginLine];
//    
//    UIView *endLine = [[UIView alloc] init];
//    endLine.frame = CGRectMake(0, KSECTIONHEIGHT-0.5, tableView.frame.size.width, 0.5);
//    [endLine setBackgroundColor:KSEPERATORLINECOLOR];
//    //    [endLine setBackgroundColor:[UIColor redColor]];
//    [headerView addSubview:endLine];
//    
//    return headerView;
//}

//- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section   // custom view for footer. will be adjusted to default or specified footer height
//{
//    UIView *headerView = nil;
//    headerView = [[UIView alloc] init];
//    headerView.frame = CGRectMake(0, 0, tableView.frame.size.width, 2);
//    headerView.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
//    UIView *beginLine = [[UIView alloc] init];
//    beginLine.frame = CGRectMake(0, 0, tableView.frame.size.width, 0.5);
//    [beginLine setBackgroundColor:KSEPERATORLINECOLOR];
//    [headerView addSubview:beginLine];
//    return headerView;
//    
//}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height = 50.0f;
    
    if(indexPath.row == 0 && indexPath.section == 0)
    {
        height = 60.0f;
    }
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    switch (indexPath.section)
    {
        case 0:
        {
            switch (indexPath.row)
            {
                case 0:
                {
                    CContactModel *model = [[[CContactMgr alloc]init] loadCurContact];
                    NSRange range = [model.UserCode rangeOfString:@"wanzhao"];
                    if ([self.contact.yuLiuZiduan3 isEqualToString:@"isKefu"]&&range.location == NSNotFound) {
                        return;
                    }
                    [self showContactDetail];
                
                }
                    
                    break;
                case 1:
                {
                    //bug fix by lixu
                    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:self.contact.messageSettingType ==  MESSAGESETTING_RECEVIEDANDREMIND ? @"接收并提示√":@"接收并提示",self.contact.messageSettingType == MESSAGESETTING_RECEVIEDNOTREMIND ? @"接收而不提示√":@"接收而不提示",self.contact.messageSettingType == MESSAGESETTING_ONLYSHOWUNREADCOUNT ?@"只显示未读消息数目√":@"只显示未读消息数目", nil];
                    [sheet showInView:self.view];
//                    __weak typeof(self) weakself = self;
//                     UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
//                    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消"style:UIAlertActionStyleCancel handler:nil];
//                    
//                    UIAlertAction *action2 = [UIAlertAction actionWithTitle:self.contact.messageSettingType ==  MESSAGESETTING_RECEVIEDANDREMIND ? @"接收并提示√":@"接收并提示" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                        weakself.contact.messageSettingType = MESSAGESETTING_RECEVIEDANDREMIND;
//                                    [weakself.contMgr updateContact:weakself.contact];
//                                    [weakself.tableView reloadData];
//                    }];
//                    
//                    UIAlertAction *action3 = [UIAlertAction actionWithTitle:self.contact.messageSettingType == MESSAGESETTING_RECEVIEDNOTREMIND ? @"接收而不提示√":@"接收而不提示" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                        weakself.contact.messageSettingType = MESSAGESETTING_RECEVIEDNOTREMIND;
//                        [weakself.contMgr updateContact:weakself.contact];
//                        [weakself.tableView reloadData];
//                    }];
//                    
//                    UIAlertAction *action4 = [UIAlertAction actionWithTitle:self.contact.messageSettingType == MESSAGESETTING_ONLYSHOWUNREADCOUNT ?@"只显示未读消息数目√":@"只显示未读消息数目" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//                        weakself.contact.messageSettingType = MESSAGESETTING_ONLYSHOWUNREADCOUNT;
//                        [weakself.contMgr updateContact:weakself.contact];
//                        [weakself.tableView reloadData];
//                    }];
//                    [actionSheet addAction:action2];
//                    [actionSheet addAction:action3];
//                    [actionSheet addAction:action4];
//                    [actionSheet addAction:action1];
//                    [self presentViewController:actionSheet animated:YES completion:nil];
//
// 
                }
                    break;
                case 2:
                {
                    IMChatRecordController *vc = [[IMChatRecordController alloc] init];
                    vc.member = self.contact;
                    vc.group = nil;
                    [self.navigationController pushViewController:vc animated:YES];
                }
                    break;

                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
}

#pragma mark actionsheedelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
        {
            self.contact.messageSettingType = MESSAGESETTING_RECEVIEDANDREMIND;
            [self.contMgr updateContact:self.contact];
            [_tableView reloadData];
        }
            break;
        case 1:
        {
            self.contact.messageSettingType = MESSAGESETTING_RECEVIEDNOTREMIND;
            [self.contMgr updateContact:self.contact];
            [_tableView reloadData];

            
        }
            break;
        case 2:
        {
            self.contact.messageSettingType = MESSAGESETTING_ONLYSHOWUNREADCOUNT;
            [self.contMgr updateContact:self.contact];
            [_tableView reloadData];
            
        }
            break;
        case 3:
        {
            
        }
            break;
        default:
            break;
    }
}


- (void)showContactDetail
{
    GroupMemberDetailVC *vc = [[GroupMemberDetailVC alloc] init];
    vc.member = self.contact;
    vc.isFromChat = YES;
    vc.isFromGroupChat = NO;
    [self.navigationController pushViewController:vc animated:YES];

}
@end
